﻿Public Class UCNha
    Private Sub UCNha_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ddlLoaiNhaLoad
        ddlDonviccLoad()
        GridView_Load
    End Sub

    Sub ddlLoaiNhaLoad()
        Dim sql As String = "select * from LoaiNha"
        ddlLoaiNha.DataSource = DataProvider.LoadTable(sql)
        ddlLoaiNha.DisplayMember = "TENLOAINHA"
        ddlLoaiNha.ValueMember = "MALOAINHA"
        ddlLoaiNha.SelectedIndex = 0
    End Sub
    Sub ddlDonviccLoad()
        Dim sql As String = "select * from DONVICUNGCAP"
        ddlDonvicc.DataSource = DataProvider.LoadTable(sql)
        ddlDonvicc.DisplayMember = "TENDV"
        ddlDonvicc.ValueMember = "MADV"
        ddlDonvicc.SelectedIndex = 0
    End Sub
    Sub GridView_Load()
        GControl.DataSource = NhaBUS.Nha_Load()
    End Sub
    Function TestInfo() As Boolean
        If txtDiachi.Text = "" Or txtDienTich.Text = "" Or txtMucGia.Text = "" _
            Or ddlDonvicc.Text = "" Or ddlLoaiNha.Text = "" Then
            ThongBao.TestInfo()
            Return False
        End If
        Return True
    End Function

    Private Sub GView_RowCellClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs) Handles GView.RowCellClick
        ddlLoaiNha.SelectedValue = GView.GetFocusedRowCellDisplayText("MALOAINHA")
        txtDienTich.Text = GView.GetFocusedRowCellDisplayText("DIENTICH")
        txtMucGia.Text = GView.GetFocusedRowCellDisplayText("MUCGIA")
        txtDiachi.Text = GView.GetFocusedRowCellDisplayText("DIACHI")
        txtManha.Text = GView.GetFocusedRowCellDisplayText("MANHA")
        txtTinhTrang.Text = GView.GetFocusedRowCellDisplayText("TINHTRANG")
        ddlDonvicc.SelectedValue = GView.GetFocusedRowCellDisplayText("MADV")
    End Sub

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        If Not TestInfo() Then
            Return
        End If
        Dim n As New NhaDTO()
        'n.Manha = ""
        n.Maloainha = ddlLoaiNha.SelectedValue
        n.Dientich = txtDienTich.Text
        n.Mucgia = txtMucGia.Text
        n.Diachi = txtDiachi.Text
        'n.Tinhtrang = 
        n.Madv = ddlDonvicc.SelectedValue
        NhaBUS.Nha_Insert(n)
        GridView_Load()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        If Not TestInfo() Then
            Return
        End If
        If Not ThongBao.Agree Then
            Return
        End If
        Dim n As New NhaDTO()
        n.Manha = GView.GetFocusedRowCellDisplayText("MANHA")
        n.Maloainha = ddlLoaiNha.SelectedValue
        n.Dientich = txtDienTich.Text
        n.Mucgia = txtMucGia.Text
        n.Diachi = txtDiachi.Text
        n.Madv = ddlDonvicc.SelectedValue
        NhaBUS.Nha_Update(n)
        GridView_Load()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Not ThongBao.Agree Then
            Return
        End If
        Dim n As New NhaDTO()
        n.Manha = GView.GetFocusedRowCellDisplayText("MANHA")
        NhaBUS.Nha_Delete(n)
        GridView_Load()
    End Sub
End Class
